const SERVICE = require("../../lib/js/service.js");

Page({
  data:{
    showItem: 0,
    money: [100,200,500,1000,2000,3000]
  },

  /*分享*/
  onShareAppMessage(){
    
  },

  getSum(e){
    let index = e.currentTarget.dataset.index;
    this.setData({
      showItem: index
    });
  },
  
  input(){
    this.setData({
      showItem: null
    });
  },

  /**
   * @explain formSubmit 充值
   * 
   * */ 
  formSubmit(e){
    let _this = this,td = this.data, 
    form_obj = e.detail.value, sum = e.detail.target.dataset.sum;
    wx.login({
      success:function(res){
        wx.setStorageSync("loginInfo", res);
        if (td.showItem === null) {
          if (!form_obj.sum) {
            wx.showToast({
              title: "请输入金额",
              icon: "none",
              mask: true
            });
          } else {
            _this.wxpayverify(form_obj.sum);
          }
        } else {
          _this.wxpayverify(sum);
        }
      }
    })
   
   
  },

  /**
   * @explain wxpayverify 微信支付
   * @param thirdCookie 用户标识
   * @param code 登录code码
   * @param price 金额
   * @param business 业务类型 （topMoney 余额 topMaster 大师币）
   * 
   * */ 
  wxpayverify(price){
    wx.showLoading({
      title: "充值中...",
      mask: true
    });
    SERVICE.POST(SERVICE.URL.wxpayverify, {
      thirdCookie: wx.getStorageSync("thirdCookie"),
      code: wx.getStorageSync("loginInfo").code,
      price: price,
      business: "topMoney"
    }, (status, res) => {
      console.log(res);
      wx.hideLoading();
      if(res.data.status === "success"){
        wx.requestPayment({
          'timeStamp': res.data.timeStamp,
          'nonceStr': res.data.nonceStr,
          'package': 'prepay_id=' + res.data.prepay_id,
          'signType': 'MD5',
          'paySign': res.data.paySign,
          'success': function (res) {
            wx.navigateBack();
          },
          'fail': function (res) {
            console.log("fail:", res);
            wx.showToast({
              title: "您取消了支付",
              icon: "none",
              mask: true,
              duration: 1500
            });
          }
        })
      }else{
        wx.showToast({
          title: res.data.message,
          icon: "none",
          mask: true
        });
      }
    });
  }
});